package org.jeecg.modules.yw.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.jeecg.modules.yw.entity.Messages;
import org.jeecg.modules.yw.entity.Order;
import org.jeecg.modules.yw.mapper.MessagesMapper;
import org.jeecg.modules.yw.mapper.OrderMapper;
import org.jeecg.modules.yw.utils.DateUtils;
import org.jeecg.modules.yw.vo.MessageVo;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

@Service
public class MessagesService extends ServiceImpl<MessagesMapper, Messages>  {
	
	
	@Resource 
	private MessagesMapper messageMapper;
	@Resource
	private OrderMapper orderMapper;
	public int IfHasMessages(String userId) {
		QueryWrapper<Messages> queryWrapper = new QueryWrapper<>();		
		queryWrapper.eq("currentUserId", userId);
	    queryWrapper.eq("delFlag", 0);
	    queryWrapper.eq("state", 0);
	    queryWrapper.orderByDesc("msgTime");		
		List<Messages> list = messageMapper.selectList(queryWrapper);		
//		if (list != null && list.size() > 0) {
//			return 1;
//		}
//		return 0;
		if (list != null && list.size() > 0) {
			return list.size();
		}
		return 0;
	}
	
	/**
	 * 处理消息
	 * @param userId
	 * @param reason
	 * @return
	 */
	public int updateMessage(String msgId, String reason,String remark) {
		return messageMapper.updateMessage(msgId, reason,remark);
	}
	
	
	/**
	 * 得到消息
	 * @param userId
	 * @param reason
	 * @return
	 */
	public List<Messages> getMessages(String orderId) {
		return messageMapper.getOrderByOrderNo(orderId);
	}
	
	
	/**
	 * 得到最新消息
	 * @param userId
	 * @param reason
	 * @return
	 */
	public List<MessageVo> getLatestMessage(String userId, Integer pageNo, Integer pageSize) {
		QueryWrapper<MessageVo> queryWrapper = new QueryWrapper<>();		
		queryWrapper.eq("m.currentUserId", userId);
	    queryWrapper.eq("m.delFlag", 0);
	    queryWrapper.eq("m.state", 0);
	    queryWrapper.orderByDesc("m.msgTime");		
	    Page<MessageVo> page = new Page<>(pageNo, pageSize);
		List<MessageVo> list = messageMapper.getAllMessages(page,queryWrapper).getRecords();
//		Messages obj = null;
//		if (list != null && list.size() > 0) {
//			obj = list.get(0);
//		}
//		return obj;
		if (list != null && list.size() > 0) {
//			obj = list.get(0);
			return list;
		}
		//这里 如果list为空 返回空列表
		return new ArrayList<>();
	}
	
	
	
	/**
	 * 结算自动确认
	 * @param userId
	 * @param reason
	 * @return
	 */
	public void jsAutoConfirm() {
		QueryWrapper<Messages> queryWrapper = new QueryWrapper<>();		
	    queryWrapper.eq("delFlag", 0);
	    queryWrapper.eq("state", 0);
	    queryWrapper.eq("orderMsgType", 7);
	    queryWrapper.orderByDesc("msgTime");
	    String dateStr = DateUtils.getCurrentTimeDateStr();
	    List<Messages> list = messageMapper.selectList(queryWrapper);
	    
	    for (Messages obj : list) {
	    	String qrTime = obj.getMsgTime();
	    	long diff = DateUtils.dateDiff(qrTime, dateStr, "yyyy-MM-dd HH:mm:ss", "day");
	    	if (diff > 2) {
                 Order order = new Order();
                 order.setId(obj.getOrderId());
                 order.setOrderStatus(10);
                 order.setJstime(DateUtils.strToDateTime(DateUtils.getCurrentTimeDateStr()));
                 orderMapper.updateOrderInfo(order);
                 
                 // 自动确认消息
                 messageMapper.confirmMessage(obj.getId(),"订单提交确认超过2天，系统自动确认");
                 
	    	}
	    	
	    }
	    
	}

}
